package com.changgou.web.order.controller;

import com.alibaba.fastjson.JSON;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.feign.CartFeign;
import com.changgou.order.pojo.OrderItem;
import com.changgou.web.order.util.CookieUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/wcart")
public class CartController {

    @Autowired
    private CartFeign cartFeign;

    //查询
    @GetMapping("/list")
    public String list(Model model,HttpServletRequest request, HttpServletResponse response) {
        CookieUtil.CartCookie(request,response,cartFeign);
        Map map = cartFeign.list();
        model.addAttribute("items", map);
        return "cart";
    }


    //添加
    @GetMapping("/add")
    @ResponseBody
    public Result<Map> add(String id, Integer num) {
        cartFeign.addCart(id, num);
        Map map = cartFeign.list();
        return new Result<>(true, StatusCode.OK, "添加购物车成功", map);
    }

    //未登录查询
    @GetMapping("/noLogin/list")
    public String noList(Model model, HttpServletRequest request) {
        String cookie = CookieUtil.readCookie(request, "cartCookie").get("cartCookie");
        Map<String, Integer> map;
        if (StringUtils.isEmpty(cookie)) {
            map = new HashMap<>();
        } else {
            map = JSON.parseObject(cookie, Map.class);
        }
        Map items = cartFeign.noList(map);
        model.addAttribute("items", items);
        return "cart";
    }


    //未登录添加
    @GetMapping("/noLogin/add")
    @ResponseBody
    public Result<Map> noAdd(String id, Integer num, HttpServletResponse response, HttpServletRequest request) {
        String cookie = CookieUtil.readCookie(request, "cartCookie").get("cartCookie");
        Map<String, Integer> map = null;
        if (StringUtils.isEmpty(cookie)) {
            map = new HashMap<>();
            map.put(id, num);
        } else {
            map = JSON.parseObject(cookie, Map.class);
            if (map.get(id) != null) {
                num = map.get(id) + num;
            }
            if (num <= 0) {
                map.remove(id);
            } else {
                map.put(id, num);
            }
        }
        CookieUtil.addCookie(response, "localhost", "/", "cartCookie", JSON.toJSONString(map).replace("\"", "'"), -1, false);
        Map items = cartFeign.noList(map);
        return new Result<>(true, StatusCode.OK, "添加购物车成功", items);
    }

}
